package id.web.faisalabdillah.xtimesheet.web.ctrl;


import id.web.faisalabdillah.xtimesheet.dao.IHolidaysDao;
import id.web.faisalabdillah.xtimesheet.domain.FileStorage;
import id.web.faisalabdillah.xtimesheet.domain.HolidaysEvent;
import id.web.faisalabdillah.xtimesheet.domain.Role;
import id.web.faisalabdillah.xtimesheet.domain.TimeSheet;
import id.web.faisalabdillah.xtimesheet.domain.TimeSheetDetail;
import id.web.faisalabdillah.xtimesheet.domain.User;
import id.web.faisalabdillah.xtimesheet.services.IFileStorageService;
import id.web.faisalabdillah.xtimesheet.services.IRoleService;
import id.web.faisalabdillah.xtimesheet.services.ITimeSheetService;
import id.web.faisalabdillah.xtimesheet.services.IUserService;
import id.web.faisalabdillah.xtimesheet.web.dto.DTOUserTest;

import java.io.ByteArrayInputStream;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Controller
@RequestMapping("/")
public class HomeController {
	
	@Autowired
	private IHolidaysDao holydayDao;
	@Autowired
	private IFileStorageService fileStorageService;
	@Autowired
	private ITimeSheetService timeSheetService;
	@Autowired
	private IUserService userService;
	@Autowired
	private IRoleService roleService;
	
	@RequestMapping("/")
	public String indexPage(){
		return "index";
	}
	
	@RequestMapping("download")
	public void download(HttpServletResponse response) throws Exception{
		DateTime date=DateTimeFormat.forPattern("MM-yyyy").parseDateTime("04-2012");
		TimeSheet ts=timeSheetService.findByDate(date.toDate());
		System.out.println(ts);
		if(ts == null){
			System.out.println("Null Data");
			return;
		}else{
			System.out.println(ts.getId().toString());
		}
		
		//		List<DTOUserTest> datas=new ArrayList<DTOUserTest>();
//		datas.add(new DTOUserTest("Faisal", "Jaksel", 22, 200000000));
//		datas.add(new DTOUserTest("Abdillah", "Bintaro", 23, 150000000));
//		datas.add(new DTOUserTest("Muhamad", "Jakarta", 22, 130000000));
//		datas.add(new DTOUserTest("Ada", "Garut", 25, 300000000));
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("timesheet", ts);
		XLSTransformer transform=new XLSTransformer();
		FileStorage fileS=fileStorageService.findByCodename("template");
		Workbook wb=transform.transformXLS(new ByteArrayInputStream(fileS.getFile().getBytes()), map);
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition","attachment; filename=FileAjaDah.xls");
		wb.write(response.getOutputStream());
	}
	
	
	@RequestMapping("dummy")
	public void dummyData(HttpServletResponse response){
		HolidaysEvent hol=new HolidaysEvent();
		hol.setDescription("Jumatan");
		hol.setTanggal(new Date());
		holydayDao.insert(hol);
		response.setContentType("text/html");
	}
	
	@RequestMapping("insertBatch")
	public void testInsert(HttpServletResponse response){
		TimeSheet ts=new TimeSheet();
		DateTime month=DateTimeFormat.forPattern("MM-yyyy").parseDateTime("04-2012");
		ts.setMonth(month.toDate());
		
		int bulan=month.monthOfYear().get();
		
		List<TimeSheetDetail> listTsd=new ArrayList<TimeSheetDetail>();
		while(bulan == month.monthOfYear().get()){
			System.out.println("tanggal:"+month.dayOfMonth().get());
			System.out.println("bulan:"+month.monthOfYear().get());
			TimeSheetDetail tsd=new TimeSheetDetail();
			tsd.setDate(month.toDate());
			tsd.setTimesheet(ts);
			listTsd.add(tsd);
			month=month.plusDays(1);
		}
		ts.setDetail(listTsd);
		timeSheetService.insert(ts);
	}
	
	@RequestMapping(value="jsonExample",method=RequestMethod.GET)
	public @ResponseBody Map<String, String> jsonExample(){
		Map<String, String> map=new HashMap<String, String>();
		map.put("test", "hasilTest");
		map.put("Umur", "12");
		return map;
	}
	
	@RequestMapping(value="createUser")
	public void createUser(HttpServletResponse response){
		User user=new User();
		user.setEmail("faitz1989@gmail.com");
		user.setActive(true);
		user.setLastLogin(new Date());
		Role role=new Role();
		role.setRoleCode("ROLE_ADMIN");
		role.setRoleDesc("Role Admin");
		role.setRoleName("ROLE ADMIN");
		user.setRoles(role);
		roleService.insert(role);
		userService.insert(user);
	}
}
